#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using System.Web;
using Agresso.Interface.Layer.Service;
using System.Collections;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class SoftmarCommissionManagersLegalEntityMappingForm : Form
    {
        CommissionManagersLegalEntity m_subject;
        UserControlCommissionManagersLegalEntityMapping m_parent;
        public SoftmarCommissionManagersLegalEntityMappingForm(CommissionManagersLegalEntity cmle, UserControlCommissionManagersLegalEntityMapping parent)
        {
            InitializeComponent();
            this.Text = parent.Name;
            m_subject = cmle;
            m_parent = parent;

            textBoxVesselSoftmar.Text = cmle.vessel_softmar;
            textBoxVesselAgresso.Text = cmle.vessel_agresso;
            textBoxVesselAgressoDimVal.Text = cmle.vessel_agresso_dim_val;

            textBoxProfitCentreSoftmar.Text = cmle.profit_centre_softmar;
            textBoxProfitCentreAgresso.Text = cmle.profit_centre_agresso;
            textBoxProfitCentreAgressoDimVal.Text = cmle.profit_centre_agresso_dim_val;

            textBoxCommercialManager.Text = cmle.commercial_manager;
            textBoxCommercialManagerDimVal.Text = cmle.commercial_manager_dim_val;

            KeyEnterEventFire(this.Controls);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateSubject();
                this.Close();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void UpdateSubject()
        {

            m_subject.vessel_softmar = textBoxVesselSoftmar.Text;
            m_subject.vessel_agresso =textBoxVesselAgresso.Text;
            m_subject.vessel_agresso_dim_val = textBoxVesselAgressoDimVal.Text;

            m_subject.profit_centre_softmar = textBoxProfitCentreSoftmar.Text;
            m_subject.profit_centre_agresso = textBoxProfitCentreAgresso.Text;
            m_subject.profit_centre_agresso_dim_val = textBoxProfitCentreAgressoDimVal.Text;

            m_subject.commercial_manager = textBoxCommercialManager.Text ;
            m_subject.commercial_manager_dim_val = textBoxCommercialManagerDimVal.Text;

            CommissionManagersLegalEntityService service = new CommissionManagersLegalEntityService();
            if(m_subject.cmle_id==0)
                service.Insert(m_subject);
            else
                service.Update(m_subject);
            m_parent.UpdateSelectedItem(m_subject);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void textBoxAgrDescription_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            buttonApply.Enabled = true;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateSubject();
                buttonApply.Enabled = false;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void SoftmarVesselMappingForm_Load(object sender, EventArgs e)
        {
            textBoxVesselSoftmar.Focus();
            textBoxVesselSoftmar.SelectAll();
        }

        private void KeyEnterEventFire(Control.ControlCollection controls)
        {
            foreach (Control c in controls)
            {
                if (c is TextBox || c is ComboBox || c is DateTimePicker || c is Button)
                {
                    c.KeyPress += new KeyPressEventHandler(c_KeyPress);
                }
                KeyEnterEventFire(c.Controls);
            }
        }

        void c_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                Control cont = (Control)sender;
                this.SelectNextControl((Control)sender, true, true, true, true);
            }
        }
    }
}